<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/******************************************************
* Profile			: menu 
* Author			: 乔伟
* Create Time		: 2011-10-05 
* Modify Time		: 2011-10-05 
* Modify Profile	:  
******************************************************/
class menu extends Admin_Controller {
	/******************************************************
	* Profile			: 列表
	* Author			: 乔伟
	* Create Time		: 2011-10-05 
	* Modify Time		: 2011-10-05
	* Modify Profile	:  
	******************************************************/ 
	public function index()
	{
		$menu_item = new menu_item_model();
		// print_r($menu_item);exit; 
		if(isset($_GET['id']))
		{
			$menu_item = $this->menu_manager->get_item($this->input->get('id'));
		}
		if($this->is_postback())
		{
			$this->load->library('form_validation');
			
			if ($this->form_validation->run('menu') == TRUE)
			{
				$post_menu_item = $this->filter_post();
				$id = $this->input->get('id');
				if(empty($id))
				{
					$id = $this->menu_manager->insert_item($post_menu_item);
				}
				else
				{
					$this->menu_manager->update_item($post_menu_item,array('id'=>$id)) ; 
				} 
				redirect(SELF."/menu/index?name=".$_GET['name']."&id=$id") ; 
			}
		} 
		$menu = $this->menu_manager->get($this->input->get('name'));
		$menu->merge($menu_item);
		// print_r($menu);exit; 
		$this->load->view("admin/menu/v_index",$menu); 
	}


	/******************************************************
	* Profile		: 编辑
	* Author		: 乔伟
	* Create Time		: 2011-10-05 
	* Modify Time		: 2011-10-05
	* Modify Profile	:  
	******************************************************/
	public function edit()
	{
		if(!$this->is_postback())
		{
			$menu_item = new menu_item_model();
			if(isset($_GET['id']))
			{
				$menu_item = $this->menu_manager->get_item($this->input->get('id'));
			}
			$this->load->view("admin/menu/v_form",$menu_item);
		}
		else
		{
			$this->load->library('form_validation');
			if ($this->form_validation->run('menu') == TRUE)
			{
				$post_menu_item = $this->filter_post();
				$id = $this->input->get('id');
				
				if(empty($id))
				{
					$this->menu_manager->insert_item($post_menu_item);
				}
				else
				{
					// echo $id;exit;
					$this->menu_manager->update_item($post_menu_item,array('id'=>$id)) ; 
				}
				$this->goto_last_index_page() ; 
			}
			$menu_item = new menu_item_model();
			if(isset($_GET['id']))
			{
				$menu_item = $this->menu_manager->get_item($this->input->get('id'));
			}
			$this->load->view("admin/menu/v_form",$menu_item);
		}
	}


	/******************************************************
	* Profile		: 删除
	* Author		: 乔伟
	* Create Time		: 2011-10-05 
	* Modify Time		: 2011-10-05
	* Modify Profile	:  
	******************************************************/
	public function del($id = null) 
	{
		$id = isset($_GET['id'])?$_GET['id']:$id;
		if(!empty($id))
		{
			$this->menu_manager->delete_item($id) ;
		}
		// print_r($_POST);exit;
		if(isset($_POST['ids']))
		{
			//echo implode(',', $this->input->post('ids')); exit ;
			foreach($this->input->post('ids') as $id)
			{
				$this->menu_manager->delete_item($id) ; 
			} 
		}
		$this->goto_last_index_page(); 
	}
	
	/******************************************************
	 * Profile		: 排序
	* Author		: 乔伟
	* Create Time		: 2012-05-14
	* Modify Time		: 2012-05-14
	* Modify Profile	:
	******************************************************/
	public function order()
	{
		//print_r($_POST['listorder']);exit;
		if(isset($_POST['listorder']) && is_array($_POST['listorder']))
		{
			foreach($this->input->post('listorder') as $k=>$v)
			{
				$this->menu_manager->update_item(array('order'=>$v),array('id'=>$k)) ; 
			}
		} 
		$this->goto_last_index_page(); 
	}
	
	
	/******************************************************
	 * Profile		: 移动到
	* Author		: 宁秀娟
	* Create Time		: 2012-06-08
	* Modify Time		: 2012-06-08
	* Modify Profile	:
	******************************************************/
	public function moveto($parent_id,$id=null)
	{
		$id = isset($_GET['id'])?$_GET['id']:$id;
		if(!empty($id))
		{
			$this->menu_manager->moveto($parent_id,$id) ;
		}
		// print_r($_POST);exit;
		if(isset($_POST['ids']))
		{
			//echo implode(',', $this->input->post('ids')); exit ;
			foreach($this->input->post('ids') as $id)
			{
				// echo $article_id . "-$class_id";
				$this->menu_manager->moveto($parent_id,$id) ;
			}
		}
		$this->goto_last_index_page();
	}
}
